import {
  getScoreListRequest,
} from '../../api/main'

const scoreCacheKey = "scores"  //最终有效成绩

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [], // 成绩列表
    termIndex: 0,// 当前学期索引
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },

  getList() {
    const cache = wx.getStorageSync(scoreCacheKey)
    if (cache) {// 缓存存在
      this.setData({
        list: cache
      })
      return
    }
    this.update()  //没有缓存就update
  },

  //刷新按钮
  update() {
    const that = this
    let p = null
    const postData = {
      useraccount: wx.getStorageSync('userInfo').useraccount,
      userpwd: wx.getStorageSync('userInfo').userpwd,
      isBind: wx.getStorageSync('userInfo').isBind,
      token: wx.getStorageSync('token'),
      team : ""
    }
    p = getScoreListRequest(postData)
    p.then(res => {
      that.setData({
        list: res.data.table
      })
      // 存入缓存
      wx.setStorageSync(scoreCacheKey, res.data.table)
    })
  },

  changeTerm(e) {
    const termIndex = e.detail.value
    this.setData({
      termIndex
    })
  }
})